﻿#region Using directives

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using NetROS.Tiers.Entities;
using NetROS.Tiers.Data;

#endregion

namespace NetROS.Tiers.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="VwReportProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract class VwReportProviderBaseCore : EntityViewProviderBase<VwReport>
	{
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions
		
		/*
		///<summary>
		/// Fill an VList&lt;VwReport&gt; From a DataSet
		///</summary>
		/// <param name="dataSet">the DataSet</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList&lt;VwReport&gt;"/></returns>
		protected static VList&lt;VwReport&gt; Fill(DataSet dataSet, VList<VwReport> rows, int start, int pagelen)
		{
			if (dataSet.Tables.Count == 1)
			{
				return Fill(dataSet.Tables[0], rows, start, pagelen);
			}
			else
			{
				return new VList<VwReport>();
			}	
		}
		
		
		///<summary>
		/// Fill an VList&lt;VwReport&gt; From a DataTable
		///</summary>
		/// <param name="dataTable">the DataTable that hold the data.</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList<VwReport>"/></returns>
		protected static VList&lt;VwReport&gt; Fill(DataTable dataTable, VList<VwReport> rows, int start, int pagelen)
		{
			int recordnum = 0;
			
			System.Collections.IEnumerator dataRows =  dataTable.Rows.GetEnumerator();
			
			while (dataRows.MoveNext() && (pagelen != 0))
			{
				if(recordnum >= start)
				{
					DataRow row = (DataRow)dataRows.Current;
				
					VwReport c = new VwReport();
					c.ID = (Convert.IsDBNull(row["ID"]))?(int)0:(System.Int32)row["ID"];
					c.Naziv = (Convert.IsDBNull(row["Naziv"]))?string.Empty:(System.String)row["Naziv"];
					c.IDParent = (Convert.IsDBNull(row["IDParent"]))?(int)0:(System.Int32?)row["IDParent"];
					c.SelectString = (Convert.IsDBNull(row["SelectString"]))?string.Empty:(System.String)row["SelectString"];
					c.Tip = (Convert.IsDBNull(row["Tip"]))?(int)0:(System.Int32)row["Tip"];
					c.Kolumne = (Convert.IsDBNull(row["Kolumne"]))?string.Empty:(System.String)row["Kolumne"];
					c.Filter = (Convert.IsDBNull(row["Filter"]))?new byte[] {}:(System.Byte[])row["Filter"];
					c.FilterAdv = (Convert.IsDBNull(row["FilterAdv"]))?new byte[] {}:(System.Byte[])row["FilterAdv"];
					c.Poredak = (Convert.IsDBNull(row["Poredak"]))?string.Empty:(System.String)row["Poredak"];
					c.AcceptChanges();
					rows.Add(c);
					pagelen -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		*/	
						
		///<summary>
		/// Fill an <see cref="VList&lt;VwReport&gt;"/> From a DataReader.
		///</summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pageLength">number of row.</param>
		///<returns>a <see cref="VList&lt;VwReport&gt;"/></returns>
		protected VList<VwReport> Fill(IDataReader reader, VList<VwReport> rows, int start, int pageLength)
		{
			int recordnum = 0;
			while (reader.Read() && (pageLength != 0))
			{
				if(recordnum >= start)
				{
					VwReport entity = null;
					if (DataRepository.Provider.UseEntityFactory)
					{
						entity = EntityManager.CreateViewEntity<VwReport>("VwReport",  DataRepository.Provider.EntityCreationalFactoryType); 
					}
					else
					{
						entity = new VwReport();
					}
					entity.ID = (System.Int32)reader["ID"];
					//entity.ID = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
					entity.Naziv = (System.String)reader["Naziv"];
					//entity.Naziv = (Convert.IsDBNull(reader["Naziv"]))?string.Empty:(System.String)reader["Naziv"];
					entity.IDParent = reader.IsDBNull(reader.GetOrdinal("IDParent")) ? null : (System.Int32?)reader["IDParent"];
					//entity.IDParent = (Convert.IsDBNull(reader["IDParent"]))?(int)0:(System.Int32?)reader["IDParent"];
					entity.SelectString = reader.IsDBNull(reader.GetOrdinal("SelectString")) ? null : (System.String)reader["SelectString"];
					//entity.SelectString = (Convert.IsDBNull(reader["SelectString"]))?string.Empty:(System.String)reader["SelectString"];
					entity.Tip = (System.Int32)reader["Tip"];
					//entity.Tip = (Convert.IsDBNull(reader["Tip"]))?(int)0:(System.Int32)reader["Tip"];
					entity.Kolumne = reader.IsDBNull(reader.GetOrdinal("Kolumne")) ? null : (System.String)reader["Kolumne"];
					//entity.Kolumne = (Convert.IsDBNull(reader["Kolumne"]))?string.Empty:(System.String)reader["Kolumne"];
					entity.Filter = reader.IsDBNull(reader.GetOrdinal("Filter")) ? null : (System.Byte[])reader["Filter"];
					//entity.Filter = (Convert.IsDBNull(reader["Filter"]))?new byte[] {}:(System.Byte[])reader["Filter"];
					entity.FilterAdv = reader.IsDBNull(reader.GetOrdinal("FilterAdv")) ? null : (System.Byte[])reader["FilterAdv"];
					//entity.FilterAdv = (Convert.IsDBNull(reader["FilterAdv"]))?new byte[] {}:(System.Byte[])reader["FilterAdv"];
					entity.Poredak = reader.IsDBNull(reader.GetOrdinal("Poredak")) ? null : (System.String)reader["Poredak"];
					//entity.Poredak = (Convert.IsDBNull(reader["Poredak"]))?string.Empty:(System.String)reader["Poredak"];
					entity.AcceptChanges();
					rows.Add(entity);
					pageLength -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		
		
		/// <summary>
		/// Refreshes the <see cref="VwReport"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="VwReport"/> object to refresh.</param>
		protected void RefreshEntity(IDataReader reader, VwReport entity)
		{
			reader.Read();
			entity.ID = (System.Int32)reader["ID"];
			//entity.ID = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
			entity.Naziv = (System.String)reader["Naziv"];
			//entity.Naziv = (Convert.IsDBNull(reader["Naziv"]))?string.Empty:(System.String)reader["Naziv"];
			entity.IDParent = reader.IsDBNull(reader.GetOrdinal("IDParent")) ? null : (System.Int32?)reader["IDParent"];
			//entity.IDParent = (Convert.IsDBNull(reader["IDParent"]))?(int)0:(System.Int32?)reader["IDParent"];
			entity.SelectString = reader.IsDBNull(reader.GetOrdinal("SelectString")) ? null : (System.String)reader["SelectString"];
			//entity.SelectString = (Convert.IsDBNull(reader["SelectString"]))?string.Empty:(System.String)reader["SelectString"];
			entity.Tip = (System.Int32)reader["Tip"];
			//entity.Tip = (Convert.IsDBNull(reader["Tip"]))?(int)0:(System.Int32)reader["Tip"];
			entity.Kolumne = reader.IsDBNull(reader.GetOrdinal("Kolumne")) ? null : (System.String)reader["Kolumne"];
			//entity.Kolumne = (Convert.IsDBNull(reader["Kolumne"]))?string.Empty:(System.String)reader["Kolumne"];
			entity.Filter = reader.IsDBNull(reader.GetOrdinal("Filter")) ? null : (System.Byte[])reader["Filter"];
			//entity.Filter = (Convert.IsDBNull(reader["Filter"]))?new byte[] {}:(System.Byte[])reader["Filter"];
			entity.FilterAdv = reader.IsDBNull(reader.GetOrdinal("FilterAdv")) ? null : (System.Byte[])reader["FilterAdv"];
			//entity.FilterAdv = (Convert.IsDBNull(reader["FilterAdv"]))?new byte[] {}:(System.Byte[])reader["FilterAdv"];
			entity.Poredak = reader.IsDBNull(reader.GetOrdinal("Poredak")) ? null : (System.String)reader["Poredak"];
			//entity.Poredak = (Convert.IsDBNull(reader["Poredak"]))?string.Empty:(System.String)reader["Poredak"];
			reader.Close();
	
			entity.AcceptChanges();
		}
		
		/*
		/// <summary>
		/// Refreshes the <see cref="VwReport"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="VwReport"/> object.</param>
		protected static void RefreshEntity(DataSet dataSet, VwReport entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ID = (Convert.IsDBNull(dataRow["ID"]))?(int)0:(System.Int32)dataRow["ID"];
			entity.Naziv = (Convert.IsDBNull(dataRow["Naziv"]))?string.Empty:(System.String)dataRow["Naziv"];
			entity.IDParent = (Convert.IsDBNull(dataRow["IDParent"]))?(int)0:(System.Int32?)dataRow["IDParent"];
			entity.SelectString = (Convert.IsDBNull(dataRow["SelectString"]))?string.Empty:(System.String)dataRow["SelectString"];
			entity.Tip = (Convert.IsDBNull(dataRow["Tip"]))?(int)0:(System.Int32)dataRow["Tip"];
			entity.Kolumne = (Convert.IsDBNull(dataRow["Kolumne"]))?string.Empty:(System.String)dataRow["Kolumne"];
			entity.Filter = (Convert.IsDBNull(dataRow["Filter"]))?new byte[] {}:(System.Byte[])dataRow["Filter"];
			entity.FilterAdv = (Convert.IsDBNull(dataRow["FilterAdv"]))?new byte[] {}:(System.Byte[])dataRow["FilterAdv"];
			entity.Poredak = (Convert.IsDBNull(dataRow["Poredak"]))?string.Empty:(System.String)dataRow["Poredak"];
			entity.AcceptChanges();
		}
		*/
			
		#endregion Helper Functions
	}//end class

	#region VwReportFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VwReport"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VwReportFilterBuilder : SqlFilterBuilder<VwReportColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VwReportFilterBuilder class.
		/// </summary>
		public VwReportFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VwReportFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VwReportFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VwReportFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VwReportFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VwReportFilterBuilder

	#region VwReportParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VwReport"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VwReportParameterBuilder : ParameterizedSqlFilterBuilder<VwReportColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VwReportParameterBuilder class.
		/// </summary>
		public VwReportParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VwReportParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VwReportParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VwReportParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VwReportParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VwReportParameterBuilder
} // end namespace
